package com.hz.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hz.web.entity.RoleMenuEntity;
import com.hz.web.service.IroleMenuService;

@Controller
@RequestMapping("roleMenu")
public class RoleMenuController {

	@Resource
	private IroleMenuService  roleMenuService;
	
	
	@RequestMapping("delRoleMenu")
	@ResponseBody
	public Map<String, Object> delRoleMenu(String menuidstr,int roleId){
		Map<String, Object> mapParms = new HashMap<String, Object>();
		String menuId[] =  menuidstr.split(",");
		List<Integer> menuIdLs =  new ArrayList<Integer>();
		
		for (int i = 0; i < menuId.length; i++) {
			menuIdLs.add(Integer.valueOf(menuId[i]));
		}
		
		mapParms.put("list", menuIdLs);
		mapParms.put("roleId", roleId);
		int rows =-1;
		try {
			rows = roleMenuService.delRoleMenu(mapParms);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", rows);
		
		return map;
	}
	
	
	/**
	 * 添加权限菜单
	 * @return
	 */
	@RequestMapping("addRoleMenu")
	@ResponseBody
	public Map<String, Object> addRoleMenu(String menuidstr,int roleId){
		
		String menuId[] =  menuidstr.split(",");
		List<RoleMenuEntity> menuIdLs =  new ArrayList<RoleMenuEntity>();
		
		for (int i = 0; i < menuId.length; i++) {
			RoleMenuEntity roleMenu = new RoleMenuEntity();
			roleMenu.setRoleId(roleId);
			roleMenu.setMenuId(Integer.valueOf(menuId[i]));
			menuIdLs.add(roleMenu);
		}
		int rows =-1;
		try {
			rows = roleMenuService.addRoleMenu(menuIdLs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", rows);
		
		return map;
	}
}
